package com.xingdata.jjxc.m.login.avt;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.ab.http.AbRequestParams;
import com.ab.http.AbStringHttpResponseListener;
import com.alibaba.fastjson.JSON;
import com.xingdata.jjxc.App;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.activity.LoginActivity;
import com.xingdata.jjxc.base.BaseActivity;
import com.xingdata.jjxc.enty.RespEntity;
import com.xingdata.jjxc.enty.UserEntity;
import com.xingdata.jjxc.utils.Code;
import com.xingdata.jjxc.utils.ExitApplication;
import com.xingdata.jjxc.utils.SP;
import com.xingdata.jjxc.utils.SystemInfo;
import com.xingdata.jjxc.views.Widget;

public class ForgetPassword2 extends BaseActivity implements
		OnClickListener {

	private EditText registertwo_pwd1, registertwo_pwd2;
	private Button registertwo_btn;

	private String phone;
	private String checknum;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		ExitApplication.getInstance().addActivity(this);
		Bundle bundle = getIntent().getExtras();
		phone = bundle.getString("phone");
		checknum = bundle.getString("checknum");

//		ExitApplication.getInstance().addActivity(this);
	}

	@Override
	protected int getContentViewId() {
		return R.layout.activity_register2;
	}

	@Override
	protected String getTitleText() {
		return "立即注册";
	}

	@Override
	protected void initViews() {
		registertwo_pwd1 = (EditText) findViewById(R.id.registertwo_pwd1_et);
		registertwo_pwd2 = (EditText) findViewById(R.id.registertwo_pwd2_et);
		registertwo_btn = (Button) findViewById(R.id.registertwo_btn);
		registertwo_btn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.registertwo_btn:
			String pswStr = registertwo_pwd1.getText().toString().trim();
			String pswRewStr = registertwo_pwd2.getText().toString().trim();

			if (pswStr.length() < 6) {
				showToast(R.string.error_pwd_length);
			} else if (!pswStr.equals(pswRewStr)) {
				showToast(R.string.error_pwd_not_same);
				registertwo_pwd2.setText("");
			} else {
				doPost_register(phone, pswStr, checknum);
			}
			break;

		default:
			break;
		}
	}

	private void doPost_register(final String phone, final String pwd,
			final String checknum) {
		if (isNetworkConnected() == false) {
			showToast(R.string.network);
			return;
		}

		resp = null;
		params = new AbRequestParams();
		params.put("mobile", phone);
		params.put("password", pwd);
		params.put("checknum", checknum);
		mAbHttpUtil.post(App.ZZD_REQUEST_REGISTER, params,
				new AbStringHttpResponseListener() {

					@Override
					public void onSuccess(int statusCode, String content) {
						resp = JSON.parseObject(content, RespEntity.class);
						Log.i("LOG", "resp : " + resp.toString());
					}

					@Override
					public void onStart() {
						showProgressDialog();
					}

					@Override
					public void onFailure(int statusCode, String content,
							Throwable error) {
					}

					@Override
					public void onFinish() {
						if (resp != null) {
							if (resp.getState() == 0) {
								showToast(R.string.register_ok);
								UserEntity entity = SystemInfo.getUserInfo();
//								entity.setPw(pwd);
								//加密之后 存储
								entity.setPw(Code
										.CodeMD5(Code.CodeMD5(pwd).toUpperCase())
										.toUpperCase());
								SP.saveUserInfo(ForgetPassword2.this,entity);
								Intent intent = new Intent(ForgetPassword2.this,LoginActivity.class);
								Bundle bundle = new Bundle();
								bundle.putString("phone", phone);
								intent.putExtras(bundle);
								startActivity(intent);
								Widget.startActivityAnim(
										ForgetPassword2.this,
										R.anim.avty_slide_in_from_right,
										R.anim.avty_slide_out_from_left);
								finish();
							} else if (resp.getState() == 1) {
								showToast("验证码超时，请重新获取");
							} else {
								showToast(resp.getMsg());
							}
						} else {
							showToast("网络超时，请重试");
							removeProgressDialog();
							return;
						}
					}

				});
	}

}
